package com.service.admin.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.news.controller.rep.NewsDetailRep;
import com.service.admin.news.controller.rep.NewsPageRep;
import com.service.admin.news.controller.req.NewsAddReq;
import com.service.admin.news.controller.req.NewsPageReq;
import com.service.admin.news.service.NewsService;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.NewsStatusEnum;
import com.service.dao.mapper.NewsMapper;
import com.service.dao.model.News;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNews(NewsAddReq newAddReq) {
        News news = ModelMapperUtil.getStrictModelMapper().map(newAddReq, News.class);
        news.setCreateTime(new Date());
        news.setNewsStatus(NewsStatusEnum.USABLE.getCode());
        this.save(news);
    }

    @Override
    public void updateNews(NewsAddReq newAddReq) {
        News news = getById(newAddReq.getId());
        if (ObjectUtils.isEmpty(news)) {
            throw new CommonException("无此系统公告");
        }
        news = ModelMapperUtil.getStrictModelMapper().map(newAddReq, News.class);
        newsMapper.updateById(news);
    }

    @Override
    public CommonPageRep<NewsPageRep> pageNews(NewsPageReq newsPageReq) {
        Page<News> page = PageHelper.startPage(newsPageReq.getPageNo(), newsPageReq.getPageSize());
        LambdaQueryWrapper<News> wrapper = new QueryWrapper<News>().lambda().orderByDesc(News::getCreateTime);
        wrapper.eq(News::getNewsStatus, CommonStatusEnums.ACTIVE.getCode());
        if (StringUtils.isNotBlank(newsPageReq.getTitle())) {
            wrapper.like(News::getNewsTitle, newsPageReq.getTitle());
        }
        if (StringUtils.isNotBlank(newsPageReq.getLang())) {
            wrapper.eq(News::getLanguage, newsPageReq.getLang());
        }
        if (!ObjectUtils.isEmpty(newsPageReq.getStartTime())) {
            wrapper.ge(News::getCreateTime, newsPageReq.getStartTime());
        }
        if (!ObjectUtils.isEmpty(newsPageReq.getEndTime())) {
            wrapper.le(News::getCreateTime, newsPageReq.getEndTime());
        }
        if (!ObjectUtils.isEmpty(newsPageReq.getIsNew())) {
            wrapper.eq(News::getIsNew, newsPageReq.getIsNew());
        }
        newsMapper.selectList(wrapper);
        List<NewsPageRep> newsPageRepList = page.getResult().stream().map(item ->
                ModelMapperUtil.getStrictModelMapper().map(item, NewsPageRep.class)).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, newsPageRepList);
    }

    @Override
    public NewsDetailRep getOneNewsDetail(Integer newsId) {
        News news = newsMapper.selectById(newsId);
        if (ObjectUtils.isEmpty(news)) {
            throw new CommonException("无此系统公告");
        }
        return ModelMapperUtil.getStrictModelMapper().map(news, NewsDetailRep.class);
    }

    @Override
    public void deleteNews(Integer newsId) {
        News news = newsMapper.selectById(newsId);
        if (ObjectUtils.isEmpty(news)) {
            throw new CommonException("无此系统公告");
        }
        news = new News();
        news.setId(newsId);
        news.setNewsStatus(CommonStatusEnums.INVALID.getCode());
        newsMapper.updateById(news);
    }
}
